import com.sun.deploy.net.proxy.ProxyUnavailableException;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

import static java.lang.Thread.sleep;


public class Test {
    public static void main(String[] args) throws InterruptedException {
        MyBlockingDeque myBlockingDeque=new MyBlockingDeque();
        //生产者 消费者模型
        Thread producer=new Thread(()->{
            try {
                for (int i = 0; i < 10; i++) {
                    System.out.println(myBlockingDeque.put(i));
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread consumerr=new Thread(()->{
            try {
                for (int i = 0; i < 10; i++) {
                    myBlockingDeque.take();
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        producer.start();
        consumerr.start();

        producer.join();
        consumerr.join();

    }
    public void Test1() {
        //休眠
        synchronized(this){
            System.out.println("线程1 加锁");
            try {
                //进入休眠
                System.out.println("进入休眠");
                this.wait();
            } catch (InterruptedException e) {
                //捕获之后继续执行
                System.out.println("捕获到了");
            }
            System.out.println("被唤醒继续执行");
        }
        System.out.println("线程1 解锁");
    }

    public void Test2(){
        synchronized (this){
            System.out.println("线程2 加锁");
            System.out.println("唤醒");
            this.notify();
            System.out.println("线程2 解锁");
        }
    }

    static Test test=new Test();
    
    public static void main4(String[] args) throws InterruptedException {
        Thread thread = new Thread() {
            @Override
            public void run() {
                test.Test1();
            }
        };

        thread.start();

        Thread.sleep(1000);
        //改变信号位，会唤醒wait，捕获到异常
        thread.interrupt();
        thread.join();

    }


    static Queue<Integer>queue=new LinkedList<Integer>();
    public static void main3(String[] args) throws InterruptedException {
        Thread thread=new Thread(){
            @Override
            public void run() {
                queue.offer(2);
            }
        };
        Thread thread1=new Thread(()->{
            queue.offer(3);
        });
        thread.start();
        thread1.start();

        thread.join();
        thread1.join();
        System.out.println(queue.poll());
        System.out.println(queue.poll());
    }
    public static void main2(String[] args) {
        Thread thread=new Thread(new Thread(){
            @Override
            public void run() {
                try {
                    //阻塞状态
                    sleep(5000);
                } catch (InterruptedException e) {
                    System.out.println("处理状态");;
                }
            }
        });
        thread.start();
        //设置状态位为true
        thread.interrupt();


    }
    public static void main1(String[] args) throws InterruptedException {
        BlockingDeque blockingDeque=new LinkedBlockingDeque();
        blockingDeque.put(1);
        blockingDeque.put(2);
        blockingDeque.put(3);

        System.out.println(blockingDeque.take());
        System.out.println(blockingDeque.take());
        System.out.println(blockingDeque.take());
        System.out.println(blockingDeque.take());


    }
}
